package com.squirrel.utils;

import java.util.Random;

/**
 * @author wangyan
 */
public class SnowFlakeIdUtil {

    private static SnowflakeIdWorker snowflakeIdWorker;
    private static SnowflakeIdWorker snowflakeIdWorker1;

    /**
     * 有极小可能造成uuid重复
     * @return
     */
    public static long getNextId() {
        if (null == snowflakeIdWorker) {
            Random random = new Random();
            int workerId = random.nextInt(32);
            int datacenterId = random.nextInt(32);
            int sequence = random.nextInt(4096);
            snowflakeIdWorker = new SnowflakeIdWorker(workerId, datacenterId, sequence);
        }
        return snowflakeIdWorker.nextId();
    }

    /**
     * @param workerId     工作ID (0~31)
     * @param datacenterId 数据中心ID (0~31)
     * @param sequence     毫秒内序列(0~4095)
     * @return
     */
    public static long getNextId(long workerId, long datacenterId, long sequence) {
        if (null == snowflakeIdWorker1) {
            snowflakeIdWorker1 = new SnowflakeIdWorker(workerId, datacenterId, sequence);
        }
        return snowflakeIdWorker1.nextId();
    }
}
